//
//  EnumerationsLearn.swift
//  BJ12366Swift
//
//  Created by ZRAR on 16/2/22.
//  Copyright © 2016年 ZRAR. All rights reserved.
//

import UIKit

class EnumerationsLearn: NSObject {
    
    enum CompassPoint{
        
        case North
        case South
        case East
        case West
    }
    //多个成员值可以出现在同一行上，用逗号隔开
    enum Planet{
        case Mercury,Venus,Earth,Mars,Jupiter,Saturn,Uranus
    }
    //关联值
    enum Barcode{
        //条形码
        case UPCA(Int,Int,Int,Int)
        //二维码
        case QRCode(String)
    }
    //原始值
    enum AsCIIControlCharacter:Character{
        case Tab = "\t"
        case LineFeed = "\n"
        case CarriageReturn = "\r"
    }
    //原始值的隐式赋值
    //当使用字符串作为枚举类型的原始值时，每个枚举成员的隐式原始值为该成员的名称
    enum CompassPointStr:String{
        
        case North,South,East,West
        
    }
    //递归枚举是一种枚举类型，它有一个或多个枚举成员使用该枚举类型的实例作为关联值，在枚举成员前加上indrect来表示该成员可递归
    enum ArithmeticExpression{
        case Number(Int)
        indirect case Addition(ArithmeticExpression,ArithmeticExpression)
        indirect case Multipliction(ArithmeticExpression,ArithmeticExpression)
    }
    var directionToHead = CompassPoint.East
    override init() {
        super.init()
        directionToHead = .North
        print("\(directionToHead)")
        //使用枚举成员的rawValue属性可以访问该枚举成员的原始值
        let direction = CompassPointStr.North.rawValue
        print(direction)
        let direction1 = CompassPointStr(rawValue: "North")
        //计算（5+4）*2
        let five = ArithmeticExpression.Number(5)
        let four = ArithmeticExpression.Number(4)
        let sum = ArithmeticExpression.Addition(five, four)
        let product = ArithmeticExpression.Addition(sum, ArithmeticExpression.Number(2))
        print("\(evaluate(product))")
        
    }
   private func evaluate(expression:ArithmeticExpression) ->Int{
        switch expression{
            
        case.Number(let value):
            return value
        case.Addition(let left, let right):
            return evaluate(left) + evaluate(right)
        case.Multipliction(let left,let right):
            return evaluate(left) * evaluate(right)
        }
    }
    var productBarcode = Barcode.UPCA(1, 2, 4, 3)
    var productBarcode2 = Barcode.QRCode("1243")
    
}
